package com.yxx.sys.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxx.data.domain.Wound;
import com.yxx.data.vo.WoundVo;
import com.yxx.sys.common.DataGridView;
import com.yxx.sys.common.Result;
import com.yxx.sys.domain.LoginInfo;
import com.yxx.sys.service.LoginInfoService;
import com.yxx.sys.vo.LoginInfoVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yxx
 * @since 2022-05-13
 */
@RestController
@RequestMapping("/loginInfo")
public class LoginInfoController {

  @Autowired
  LoginInfoService loginInfoService;

  @GetMapping("/loadAllLoginInfo")
  @ApiOperation("查询登录信息")
  public DataGridView loadAllWound(LoginInfoVO loginInfoVO){
    IPage<LoginInfo> page = new Page<LoginInfo>(loginInfoVO.getPage(),loginInfoVO.getLimit());
    QueryWrapper<LoginInfo> queryWrapper = new QueryWrapper<LoginInfo>();

    queryWrapper.like(StrUtil.isNotBlank(loginInfoVO.getLoginName()),"login_name",loginInfoVO.getLoginName());
    queryWrapper.like(StrUtil.isNotBlank(loginInfoVO.getLoginIp()),"login_ip",loginInfoVO.getLoginIp());
    queryWrapper.ge(loginInfoVO.getStartTime() != null, "login_time", loginInfoVO.getStartTime());
    queryWrapper.le(loginInfoVO.getEndTime() != null, "login_time", loginInfoVO.getEndTime());
    queryWrapper.orderByDesc("login_time");
    loginInfoService.page(page,queryWrapper);
    return new DataGridView(page.getTotal(),page.getRecords());
  }
  /**
   *@Description: 删除
   * @param id
   * @return: com.yxx.sys.common.Result
   **/
  @PostMapping("/deleteLoginInfo")
  @ApiOperation("删除登录信息")
  public Result deleteLoginInfo(Integer id) {
    try{
      loginInfoService.removeById(id);
      return Result.DELETE_SUCCESS;
    }catch(Exception e){
      e.printStackTrace();
      return Result.DELETE_SUCCESS;
    }
  }
  /**
   *@Description: 删除
   * @param loginInfoVO
   * @return: com.yxx.sys.common.Result
   **/
  @PostMapping("/batchDeleteLoginInfo")
  @ApiOperation("登录日志批量删除")
  public Result batchDeleteLoginInfo(LoginInfoVO loginInfoVO) {
    try{
      loginInfoService.removeByIds(Arrays.asList(loginInfoVO.getIds()));
      return Result.DELETE_SUCCESS;
    }catch(Exception e){
      e.printStackTrace();
      return Result.DELETE_SUCCESS;
    }
  }
}
